<?php
class Wee_Logging_Model_Event_Changes extends Mage_Core_Model_Abstract{
    const XML_PATH_SKIP_GLOBAL_FIELDS = 'adminhtml/wee/logging/skip_fields';
    protected $_globalSkipFields = array();
    protected $_skipFields = array();
    protected $_difference = null;
    public function _construct(){
        $this -> _globalSkipFields = array_map('trim', array_filter(explode(',', (string)Mage :: getConfig() -> getNode(self :: XML_PATH_SKIP_GLOBAL_FIELDS))));
        $this -> _init('wee_logging/event_changes');
    }
    protected function _beforeSave(){
        $this -> _calculateDifference();
        $this -> setOriginalData(serialize($this -> getOriginalData()));
        $this -> setResultData(serialize($this -> getResultData()));
        return parent :: _beforeSave();
    }
    public function hasDifference(){
        $difference = $this -> _calculateDifference();
        return !empty($difference);
    }
    protected function _calculateDifference(){
        if (is_null($this -> _difference)){
            $updatedParams = $newParams = $sameParams = $difference = array();
            $newOriginalData = $origData = $this -> getOriginalData();
            $newResultData = $resultData = $this -> getResultData();
            if (!is_array($origData)){
                $origData = array();
            }
            if (!is_array($resultData)){
                $resultData = array();
            }
            if (!$origData && $resultData){
                $newOriginalData = array('__was_created' => true);
                $difference = $resultData;
            }elseif ($origData && !$resultData){
                $newResultData = array('__was_deleted' => true);
                $difference = $origData;
            }elseif ($origData && $resultData){
                $newParams = array_diff_key($resultData, $origData);
                $sameParams = array_intersect_key($origData, $resultData);
                foreach ($sameParams as $key => $value){
                    if ($origData[$key] != $resultData[$key]){
                        $updatedParams[$key] = $resultData[$key];
                    }
                }
                $newOriginalData = array_intersect_key($origData, $updatedParams);
                $difference = $newResultData = array_merge($updatedParams, $newParams);
                if ($difference && !$updatedParams){
                    $newOriginalData = array('__no_changes' => true);
                }
            }
            $this -> setOriginalData($newOriginalData);
            $this -> setResultData($newResultData);
            $this -> _difference = $difference;
        }
        return $this -> _difference;
    }
    public function cleanupData($skipFields){
        if ($skipFields && is_array($skipFields)){
            $this -> _skipFields = $skipFields;
        }
        $this -> setOriginalData($this -> _cleanupData($this -> getOriginalData()));
        $this -> setResultData($this -> _cleanupData($this -> getResultData()));
    }
    protected function _cleanupData($data){
        if (!$data || !is_array($data)){
            return array();
        }
        $skipFields = $this -> _skipFields;
        if (!$skipFields || !is_array($skipFields)){
            $skipFields = array();
        }
        $clearedData = array();
        foreach ($data as $key => $value){
            if (!in_array($key, $this -> _globalSkipFields) && !in_array($key, $skipFields) && !is_array($value) && !is_object($value)){
                $clearedData[$key] = $value;
            }
        }
        return $clearedData;
    }
    public function setModelName($modelName){
        $this -> setSourceName($modelName);
    }
    public function getModelName(){
        return $this -> getSourceName();
    }
    public function setModelId($modelId){
        $this -> setSourceId($modelId);
    }
    public function getModelId(){
        return $this -> getSourceId();
    }
}
